package com.hengpeng.itfinoss.service.stm.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.QueryResult;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.model.Order.OrderType;
import com.hengpeng.itfinbase.persist.EmailLog;
import com.hengpeng.itfinbase.persist.EmailLog.EmailState;
import com.hengpeng.itfinbase.persist.EmailLog.EmailType;
import com.hengpeng.itfinoss.service.ServiceFactory;
import com.hengpeng.itfinoss.service.stm.EmailLogService;

/**
 * 邮件记录服务接口实现
 * @author lanmingming
 */

@Service
@Lazy
public class EmailLogServiceImpl implements EmailLogService
{

	@Autowired
	private ServiceFactory serviceFactory;

	@Override
	public ServiceResult<EmailLog> add(EmailLog emailLog)
	{
		EmailLog obj = serviceFactory.getPersistService().save(emailLog);
		return new ServiceResult<EmailLog>(obj);
	}

	@Override
	public ServiceResult<EmailLog> modify(EmailLog emailLog)
	{
		EmailLog obj = serviceFactory.getPersistService().update(emailLog);
		return new ServiceResult<EmailLog>(obj);
	}

	@Override
	public ServiceResult<Long> selectCountEmailLogs(EmailType emailType, String userid, String storeId, String email,
			String title, String content, EmailState state, Date sendTime1, Date sendTime2, Date successTime1,
			Date successTime2, Integer retryCount)
	{
		Long count = 0L;// serviceFactory.getDaoFactory().getEmailLogDao().selectCountEmailLogs(emailType, userid,
						// storeId, email, title, content, state, sendTime1, sendTime2, successTime1, successTime2,
						// retryCount);
		return new ServiceResult<Long>(count);
	}

	@Override
	public ServiceResult<List<EmailLog>> selectEmailLogPage(EmailType emailType, String userid, String storeId,
			String email, String title, String content, EmailState state, Date sendTime1, Date sendTime2,
			Date successTime1, Date successTime2, Integer retryCount, int indexPage, int pageSize, OrderType order,
			String orderField)
	{
		DynamicQuery query = new DynamicQuery(EmailLog.class);
		if (emailType != null && emailType.getText().trim().length() > 0)
			query.eq("type", emailType);
		if (userid != null && userid.trim().length() > 0)
			query.eq("sendUserName", userid);
		if (storeId != null && storeId.trim().length() > 0)
			query.eq("storeId", storeId);
		if (email != null && email.trim().length() > 0)
			query.like("email", "%" + email + "%");
		if (title != null && title.trim().length() > 0)
			query.like("title", "%" + title + "%");
		if (content != null && content.trim().length() > 0)
			query.like("content", "%" + content + "%");
		if (state != null && state.getText().trim().length() > 0)
			query.eq("state", state);
		if (sendTime1 != null && sendTime2 != null && sendTime1.toString().trim().length() > 0
				&& sendTime2.toString().trim().length() > 0)
			query.between("sendTime", sendTime1, sendTime2);
		if (successTime1 != null && successTime2 != null && successTime1.toString().trim().length() > 0
				&& successTime1.toString().trim().length() > 0)
			query.between("successTime", successTime1, successTime2);
		if (retryCount != null && retryCount > 0)
			query.eq("retryCount", retryCount);
		if (order != null)
		{
			if (order.name().equalsIgnoreCase("asc"))
			{
				query.asc(orderField);
			}
			else
			{
				query.desc(orderField);
			}
		}
		query.setIsSearchTotalCount(true);
		query.setPageIndex(indexPage);
		query.setPageSize(pageSize);
		QueryResult<EmailLog> requery = serviceFactory.getPersistService().findWithPage(EmailLog.class, query);
		ServiceResult<List<EmailLog>> result = new ServiceResult<List<EmailLog>>(requery.getResults());
		result.setReturnListSize((int) requery.getTotalCount());
		return result;
	}

	@Override
	public ServiceResult<List<EmailLog>> findNotsendEmailLogsOderByEmailLevel(Long[] ids, EmailState state,
			OrderType order, String orderField)
	{
		DynamicQuery query = new DynamicQuery(EmailLog.class);
		if (ids != null && ids.length > 0)
			query.inArray("id", ids);
		if (state != null && state.getText().trim().length() > 0)
			query.eq("state", state);
		if (order != null && order.name().equalsIgnoreCase("asc"))
		{
			query.asc(orderField);
		}
		else
		{
			query.desc(orderField);
		}
		QueryResult<EmailLog> requery = serviceFactory.getPersistService().findWithPage(EmailLog.class, query);
		return new ServiceResult<List<EmailLog>>(requery.getResults());
	}

}
